import React, { Component } from 'react';
import { Radio } from 'antd';
import Label from '../LabelDecorator';

@Label()
class FormRadio extends Component {
  renderStaticOptions = style => {
    const options = this.props.getData('datasource').value;
    if (!Array.isArray(options)) {
      return null;
    }
    return options.map(option => (
      <Radio
        style={style} value={option}
        key={option}
      >
        {option}
      </Radio>
    ));
  };

  render() {
    const { getData } = this.props;
    const isStatic = getData('datasource').type === 'static';
    const defaultValue = getData('defaultValue');
    const direction = getData('direction');
    const radioStyle =
      direction === 'vertical'
        ? {
          display: 'block',
          height: '30px',
          lineHeight: '30px',
        }
        : undefined;
    return (
      <Radio.Group defaultValue={defaultValue}>
        {isStatic ? this.renderStaticOptions(radioStyle) : null}
      </Radio.Group>
    );
  }
}

export default FormRadio;
